package com.example.Service;

import com.example.utils.JsonUtils;
import com.example.utils.VivoAuth;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;

/**
 * ai绘画的主要接口
 */
@Service
public class PaintingService {


    public String task_submit(String user ,String prompt) throws UnsupportedEncodingException, UnsupportedEncodingException {
        final String APP_ID = "2025985389";
        final String APP_KEY = "eAkRKtYXOawsmtVv";
        final String METHOD = "POST";
        final String URI = "/api/v1/task_submit";
        final String DOMAIN = "api-ai.vivo.com.cn";

        // 定义请求体数据
        Map<String, Object> data = new HashMap<>();
        data.put("height", 768);
        data.put("width", 576);
        data.put("prompt", user+prompt);
        data.put("styleConfig", "55c682d5eeca50d4806fd1cba3628781");
        // 将请求体数据转换为 JSON 格式
        String requestBody = JsonUtils.toJson(data);
        // 请求参数
        Map<String, Object> query = new HashMap<>();
        String queryString = mapToQueryString(query);
        HttpHeaders headers = VivoAuth.generateAuthHeaders(APP_ID, APP_KEY, METHOD, URI, queryString);
        headers.add("Content-Type", "application/json");
        String url = String.format("http://%s%s", DOMAIN, URI);
        RestTemplate restTemplate = new RestTemplate();
        HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
        if (response.getStatusCode() == HttpStatus.OK) {
            System.out.println("Response body: " + response.getBody());
        } else {
            System.out.println("Error response: " + response.getStatusCode());
        }
        return response.getBody();
    }



    public String taskProgress(String text) throws UnsupportedEncodingException {
        final String APP_ID = "2025985389";
        final String APP_KEY = "eAkRKtYXOawsmtVv";
        final String METHOD = "GET";
        final String URI = "/api/v1/task_progress";
        final String DOMAIN = "api-ai.vivo.com.cn";

        //请求url参数
        Map<String, Object> data = new HashMap<>();
        data.put("task_id",text);
        String queryString = mapToQueryString(data);
        HttpHeaders headers = VivoAuth.generateAuthHeaders(APP_ID, APP_KEY, METHOD, URI, queryString);
        String url = String.format("http://%s%s?%s", DOMAIN, URI,queryString);
        headers.add("Content-Type", "application/json");
        RestTemplate restTemplate = new RestTemplate();
        HttpEntity requestEntity = new HttpEntity<>(headers);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
        if (response.getStatusCode() == HttpStatus.OK) {
            System.out.println("Response body: " + response.getBody());
        } else {
            System.out.println("Error response: " + response.getStatusCode());
        }
        return response.getBody();
    }




    public static String mapToQueryString(Map<String, Object> map) {
        if (map.isEmpty()) {
            return "";
        }
        StringBuilder queryStringBuilder = new StringBuilder();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (queryStringBuilder.length() > 0) {
                queryStringBuilder.append("&");
            }
            queryStringBuilder.append(entry.getKey());
            queryStringBuilder.append("=");
            queryStringBuilder.append(entry.getValue());
        }
        return queryStringBuilder.toString();
    }

}
